﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
namespace BT_web__nhom06.VanPhongDoan
{
    public partial class Them_CD : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsValid == false) //neu 1 trong cac validator kiem tra khong hop le thi IsValid ==false
            {
                return;
            }
            string machidoan = txt_machidoan.Text;
            string tenchidoan = txttenchidoan.Text;
            string masvbithuchidoan = "";
            string masvphobithuchidoan = "";
                if (txtphobithuchidoan.Text != "")
                    masvphobithuchidoan = txtphobithuchidoan.Text;
                if (txtbithuchidoan.Text != "")
                    masvphobithuchidoan = txtbithuchidoan.Text;
                string daccdiem = txtdacdiemchidoan.Text;
                int ketqua = 0;
                string lenhsql = "insert into [chidoan]([machidoan],[tenchidoan],[masvbithuchidoan],[masvphobithuchidoan],[dacdiem]) values(@machidoan,@tenchidoan,@masvbithuchidoan,@masvphobithuchidoan,@dacdiem)";
                ketqua = Tienichsql.ketnoicsdl.chaylenhcapnhatdulieu(lenhsql, new string[] { "@machidoan","@tenchidoan", "@masvbithuchidoan", "@masvphobithuchidoan", "@dacdiem" }, new object[] { machidoan,tenchidoan, masvbithuchidoan, masvphobithuchidoan, daccdiem });
                if (ketqua == 1)
                {
                    Label1.Text = "Thêm  thành công!";
                    txt_machidoan.Text = "";
                    txttenchidoan.Text = "";
                    txtbithuchidoan.Text = "";
                    txtphobithuchidoan.Text = "";
                    txtdacdiemchidoan.Text = "";
                }
                 
                else
                    Label1.Text = "Thêm không thành công!";
        }
        
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string masinhvien = txtbithuchidoan.Text;
            string sqlSelect = "SELECT * FROM [doanvien],[chidoan] WHERE [doanvien].[masinhvien]=@masinhvien and [doanvien].[masinhvien]=[chidoan].[masvbithuchidoan] ";
            DataTable tblKetQua = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlSelect, "@masinhvien", masinhvien);
            if (tblKetQua.Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
                CustomValidator1.ErrorMessage = "Mã sinh viên không tồn tại";
            }
        }
        protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (txtphobithuchidoan.Text != "")
            {
                string masinhvien = txtbithuchidoan.Text;
                string sqlSelect = "SELECT * FROM [doanvien],[chidoan] WHERE [doanvien].[masinhvien]=@masinhvien and [doanvien].[masinhvien]=[chidoan].[masvphobithuchidoan] ";
                DataTable tblKetQua = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlSelect, "@masinhvien", masinhvien);
                if (tblKetQua.Rows.Count > 0)
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                    CustomValidator3.ErrorMessage = "Mã sinh viên không tồn tại";
                }

            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("QL_ChiDoan.aspx");
        }

        protected void CustomValidator4_ServerValidate1(object source, ServerValidateEventArgs args)
        {
            string machidoan = (string)txt_machidoan.Text;
            string sqlselect = "select * from chidoan where machidoan = @machidoan";
            DataTable tblmachidoan = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlselect, "@machidoan", machidoan);
            if (tblmachidoan.Rows.Count > 0)
            {
                args.IsValid = false;
                CustomValidator4.ErrorMessage = "Mã chi đoàn đã tồn tại!";
            }
                
            else
                args.IsValid = true;
        }
        
    }
}